﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Globalization;

namespace exsportance.Silverlight.Validate.Validators
{
    /// <summary>
    /// Validator that returns true if value equals given value
    /// </summary>
    public class EqualsValidator<T> : BaseValidator where T : IEquatable<T>
    {
        #region members
        /// <summary>
        /// Validation failure message key for when the value not equals
        /// </summary>
        private const string NOT_SAME = "notSame";

        /// <summary>
        /// Validation failure message key for when the value is not the right type
        /// </summary>
        private const string NOT_TYPE = "notType";

        protected Dictionary<string, string> _messagesTemplates = new Dictionary<string, string>(){
        {EqualsValidator<T>.NOT_SAME,"Tokens do not match"},
           {EqualsValidator<T>.NOT_TYPE,"'%value%' is not type of '%inputtype%'"},
        };

        #endregion

        #region properties
        /// <summary>
        /// Token value
        /// </summary>
        public T Token { get; set; }

        /// <summary>
        /// Input type
        /// </summary>
        public string InputType { get { return typeof(T).Name; } }
        #endregion

        #region constructor
        public EqualsValidator(T token)
            : base()
        {
            Token = token;
        }

        #endregion

        #region protected methods
        /// <summary>
        /// Returns true if if value equals token
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected override bool CheckValid(object value)
        {
            rememberValue(value);

            if (value == null && Token == null)
                return true;
            else
            {
                T convertedValue = default(T);

                try
                {
                    convertedValue = (T)Convert.ChangeType(value, typeof(T), CultureInfo.CurrentCulture.NumberFormat);
                }
                catch
                {
                    raiseMessage(EqualsValidator<T>.NOT_TYPE);
                    return false;
                }

                if (convertedValue != null && Token != null)
                {
                    if (!convertedValue.Equals(Token))
                    {
                        raiseMessage(EqualsValidator<T>.NOT_SAME);
                        return false;
                    }
                }
                else
                {
                    raiseMessage(EqualsValidator<T>.NOT_SAME);
                    return false;
                }
            }

            return true;
        }

        protected override System.Collections.Generic.Dictionary<string, string> getMessageTemplates()
        {
            return _messagesTemplates;
        }
        #endregion
    }
}
